home *** CD-ROM | disk | FTP | other *** search
/ AGA Toolkit '97 / The AGA Toolkit '97.iso / programming / c / make-3.75 / remake.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-07  |  27.4 KB  |  1,091 lines

  1. /* Basic dependency engine for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "commands.h"
  21. #include "job.h"
  22. #include "dep.h"
  23. #include "file.h"
  24. #include <assert.h>
  25.  
  26. #ifdef HAVE_FCNTL_H
  27. #include <fcntl.h>
  28. #else
  29. #include <sys/file.h>
  30. #endif
  31.  
  32. extern int try_implicit_rule ();
  33.  
  34.  
  35. /* Incremented when a command is started (under -n, when one would be).  */
  36. unsigned int commands_started = 0;
  37.  
  38. static int update_file (), update_file_1 (), check_dep (), touch_file ();
  39. static void remake_file ();
  40. static time_t name_mtime ();
  41. static int library_search ();
  42. extern time_t f_mtime ();
  43.  
  44. /* Remake all the goals in the `struct dep' chain GOALS.  Return -1 if nothing
  45.    was done, 0 if all goals were updated successfully, or 1 if a goal failed.
  46.    If MAKEFILES is nonzero, these goals are makefiles, so -t, -q, and -n should
  47.    be disabled for them unless they were also command-line targets, and we
  48.    should only make one goal at a time and return as soon as one goal whose
  49.    `changed' member is nonzero is successfully made.  */
  50.  
  51. int
  52. update_goal_chain (goals, makefiles)
  53.      register struct dep *goals;
  54.      int makefiles;
  55. {
  56.   int t = touch_flag, q = question_flag, n = just_print_flag;
  57.   unsigned int j = job_slots;
  58.   int status = -1;
  59.  
  60. #define MTIME(file) (makefiles ? file_mtime_no_search (file) \
  61.              : file_mtime (file))
  62.  
  63.   /* Duplicate the chain so we can remove things from it.  */
  64.  
  65.   goals = copy_dep_chain (goals);
  66.  
  67.   {
  68.     /* Clear the `changed' flag of each goal in the chain.
  69.        We will use the flag below to notice when any commands
  70.        have actually been run for a target.  When no commands
  71.        have been run, we give an "up to date" diagnostic.  */
  72.  
  73.     struct dep *g;
  74.     for (g = goals; g != 0; g = g->next)
  75.       g->changed = 0;
  76.   }
  77.  
  78.   if (makefiles)
  79.     /* Only run one job at a time.  */
  80.     job_slots = 1;
  81.  
  82.   /* Update all the goals until they are all finished.    */
  83.  
  84.   while (goals != 0)
  85.     {
  86.       register struct dep *g, *lastgoal;
  87.  
  88.       /* Start jobs that are waiting for the load to go down.  */
  89.  
  90.       start_waiting_jobs ();
  91.  
  92.       /* Wait for a child to die.  */
  93.  
  94.       reap_children (1, 0);
  95.  
  96.       lastgoal = 0;
  97.       g = goals;
  98.       while (g != 0)
  99.     {
  100.       /* Iterate over all double-colon entries for this file.  */
  101.       struct file *file = g->file;
  102.       int stop, any_not_updated = 0;
  103.       for (file = g->file->double_colon ? g->file->double_colon : g->file;
  104.            file != NULL;
  105.            file = file->prev)
  106.         {
  107.           unsigned int ocommands_started;
  108.           int x;
  109.           time_t mtime = MTIME (file);
  110.           check_renamed (file);
  111.  
  112.           if (makefiles)
  113.         {
  114.           if (file->cmd_target)
  115.             {
  116.               touch_flag = t;
  117.               question_flag = q;
  118.               just_print_flag = n;
  119.             }
  120.           else
  121.             touch_flag = question_flag = just_print_flag = 0;
  122.         }
  123.  
  124.           /* Save the old value of `commands_started' so we can compare
  125.          later.  It will be incremented when any commands are
  126.          actually run.    */
  127.           ocommands_started = commands_started;
  128.  
  129.           x = update_file (file, makefiles ? 1 : 0);
  130.           check_renamed (file);
  131.  
  132.           /* Set the goal's `changed' flag if any commands were started
  133.          by calling update_file above.    We check this flag below to
  134.          decide when to give an "up to date" diagnostic.  */
  135.           g->changed += commands_started - ocommands_started;
  136.  
  137.           stop = 0;
  138.           if (x != 0 || file->updated)
  139.         {
  140.           /* If STATUS was not already 1, set it to 1 if
  141.              updating failed, or to 0 if updating succeeded.
  142.              Leave STATUS as it is if no updating was done.  */
  143.  
  144.           if (status < 1)
  145.             {
  146.               if (file->update_status != 0)
  147.             {
  148.               /* Updating failed, or -q triggered.
  149.                  The STATUS value tells our caller which.  */
  150.               status = file->update_status;
  151.               /* If -q just triggered, stop immediately.
  152.                  It doesn't matter how much more we run,
  153.                  since we already know the answer to return.  */
  154.               stop = (!keep_going_flag && !question_flag
  155.                   && !makefiles);
  156.             }
  157.               else if (MTIME (file) != mtime)
  158.             {
  159.               /* Updating was done.  If this is a makefile and
  160.                  just_print_flag or question_flag is set
  161.                  (meaning -n or -q was given and this file was
  162.                  specified as a command-line target), don't
  163.                  change STATUS.  If STATUS is changed, we will
  164.                  get re-exec'd, and fall into an infinite loop.  */
  165.               if (!makefiles
  166.                   || (!just_print_flag && !question_flag))
  167.                 status = 0;
  168.               if (makefiles && file->dontcare)
  169.                 /* This is a default makefile.  Stop remaking.  */
  170.                 stop = 1;
  171.             }
  172.             }
  173.         }
  174.  
  175.           /* Keep track if any double-colon entry is not finished.
  176.          When they are all finished, the goal is finished.  */
  177.           any_not_updated |= !file->updated;
  178.  
  179.           if (stop)
  180.         break;
  181.         }
  182.  
  183.       /* Reset FILE since it is null at the end of the loop.  */
  184.       file = g->file;
  185.  
  186.       if (stop || !any_not_updated)
  187.         {
  188.           /* If we have found nothing whatever to do for the goal,
  189.          print a message saying nothing needs doing.  */
  190.  
  191.           if (!makefiles
  192.           /* If the update_status is zero, we updated successfully
  193.              or not at all.  G->changed will have been set above if
  194.              any commands were actually started for this goal.    */
  195.           && file->update_status == 0 && !g->changed
  196.           /* Never give a message under -s or -q.  */
  197.           && !silent_flag && !question_flag)
  198.         {
  199.           if (file->phony || file->cmds == 0)
  200.             message ("Nothing to be done for `%s'.",
  201.                  file->name);
  202.           else
  203.             message ("`%s' is up to date.", file->name);
  204.           fflush (stdout);
  205.         }
  206.  
  207.           /* This goal is finished.  Remove it from the chain.  */
  208.           if (lastgoal == 0)
  209.         goals = g->next;
  210.           else
  211.         lastgoal->next = g->next;
  212.  
  213.           /* Free the storage.  */
  214.           free ((char *) g);
  215.  
  216.           g = lastgoal == 0 ? goals : lastgoal->next;
  217.  
  218.           if (stop)
  219.         break;
  220.         }
  221.       else
  222.         {
  223.           lastgoal = g;
  224.           g = g->next;
  225.         }
  226.     }
  227.     }
  228.  
  229.   if (makefiles)
  230.     {
  231.       touch_flag = t;
  232.       question_flag = q;
  233.       just_print_flag = n;
  234.       job_slots = j;
  235.     }
  236.  
  237.   return status;
  238. }
  239.  
  240. /* If FILE is not up to date, execute the commands for it.
  241.    Return 0 if successful, 1 if unsuccessful;
  242.    but with some flag settings, just call `exit' if unsuccessful.
  243.  
  244.    DEPTH is the depth in recursions of this function.
  245.    We increment it during the consideration of our dependencies,
  246.    then decrement it again after finding out whether this file
  247.    is out of date.
  248.  
  249.    If there are multiple double-colon entries for FILE,
  250.    each is considered in turn.    */
  251.  
  252. static int
  253. update_file (file, depth)
  254.      struct file *file;
  255.      unsigned int depth;
  256. {
  257.   register int status = 0;
  258.   register struct file *f;
  259.  
  260.   for (f = file->double_colon ? file->double_colon : file; f != 0; f = f->prev)
  261.     {
  262.       status |= update_file_1 (f, depth);
  263.       check_renamed (f);
  264.  
  265.       if (status != 0 && !keep_going_flag)
  266.     return status;
  267.  
  268.       switch (f->command_state)
  269.     {
  270.     case cs_finished:
  271.       /* The file is done being remade.  */
  272.       break;
  273.  
  274.     case cs_running:
  275.     case cs_deps_running:
  276.       /* Don't run the other :: rules for this
  277.          file until this rule is finished.    */
  278.       return 0;
  279.  
  280.     default:
  281.       assert (f->command_state == cs_running);
  282.       break;
  283.     }
  284.     }
  285.  
  286.   return status;
  287. }
  288.  
  289. /* Consider a single `struct file' and update it as appropriate.  */
  290.  
  291. static int
  292. update_file_1 (file, depth)
  293.      struct file *file;
  294.      unsigned int depth;
  295. {
  296.   register time_t this_mtime;
  297.   int noexist, must_make, deps_changed;
  298.   int dep_status = 0;
  299.   register struct dep *d, *lastd;
  300.   int running = 0;
  301.  
  302.   DEBUGPR ("Considering target file `%s'.\n");
  303.  
  304.   if (file->updated)
  305.     {
  306.       if (file->update_status > 0)
  307.     {
  308.       DEBUGPR ("Recently tried and failed to update file `%s'.\n");
  309.       return file->update_status;
  310.     }
  311.  
  312.       DEBUGPR ("File `%s' was considered already.\n");
  313.       return 0;
  314.     }
  315.  
  316.   switch (file->command_state)
  317.     {
  318.     case cs_not_started:
  319.     case cs_deps_running:
  320.       break;
  321.     case cs_running:
  322.       DEBUGPR ("Still updating file `%s'.\n");
  323.       return 0;
  324.     case cs_finished:
  325.       DEBUGPR ("Finished updating file `%s'.\n");
  326.       return file->update_status;
  327.     default:
  328.       abort ();
  329.     }
  330.  
  331.   ++depth;
  332.  
  333.   /* Notice recursive update of the same file.    */
  334.   file->updating = 1;
  335.  
  336.   /* Looking at the file's modtime beforehand allows the possibility
  337.      that its name may be changed by a VPATH search, and thus it may
  338.      not need an implicit rule.  If this were not done, the file
  339.      might get implicit commands that apply to its initial name, only
  340.      to have that name replaced with another found by VPATH search.  */
  341.  
  342.   this_mtime = file_mtime (file);
  343.   check_renamed (file);
  344.   noexist = this_mtime == (time_t) -1;
  345.   if (noexist)
  346.     DEBUGPR ("File `%s' does not exist.\n");
  347.  
  348.   must_make = noexist;
  349.  
  350.   /* If file was specified as a target with no commands,
  351.      come up with some default commands.  */
  352.  
  353.   if (!file->phony && file->cmds == 0 && !file->tried_implicit)
  354.     {
  355.       if (try_implicit_rule (file, depth))
  356.     DEBUGPR ("Found an implicit rule for `%s'.\n");
  357.       else
  358.     DEBUGPR ("No implicit rule found for `%s'.\n");
  359.       file->tried_implicit = 1;
  360.     }
  361.   if (file->cmds == 0 && !file->is_target
  362.       && default_file != 0 && default_file->cmds != 0)
  363.     {
  364.       DEBUGPR ("Using default commands for `%s'.\n");
  365.       file->cmds = default_file->cmds;
  366.     }
  367.  
  368.   /* Update all non-intermediate files we depend on, if necessary,
  369.      and see whether any of them is more recent than this file.  */
  370.  
  371.   lastd = 0;
  372.   d = file->deps;
  373.   while (d != 0)
  374.     {
  375.       time_t mtime;
  376.  
  377.       check_renamed (d->file);
  378.  
  379.       mtime = file_mtime (d->file);
  380.       check_renamed (d->file);
  381.  
  382.       if (d->file->updating)
  383.     {
  384.       error ("Circular %s <- %s dependency dropped.",
  385.          file->name, d->file->name);
  386.       if (lastd == 0)
  387.         {
  388.           file->deps = d->next;
  389.           free ((char *) d);
  390.           d = file->deps;
  391.         }
  392.       else
  393.         {
  394.           lastd->next = d->next;
  395.           free ((char *) d);
  396.           d = lastd->next;
  397.         }
  398.       continue;
  399.     }
  400.  
  401.       d->file->parent = file;
  402.       dep_status |= check_dep (d->file, depth, this_mtime, &must_make);
  403.       check_renamed (d->file);
  404.  
  405.       {
  406.     register struct file *f = d->file;
  407.     if (f->double_colon)
  408.       f = f->double_colon;
  409.     do
  410.       {
  411.         running |= (f->command_state == cs_running
  412.             || f->command_state == cs_deps_running);
  413.         f = f->prev;
  414.       }
  415.     while (f != 0);
  416.       }
  417.  
  418.       if (dep_status != 0 && !keep_going_flag)
  419.     break;
  420.  
  421.       if (!running)
  422.     d->changed = file_mtime (d->file) != mtime;
  423.  
  424.       lastd = d;
  425.       d = d->next;
  426.     }
  427.  
  428.   /* Now we know whether this target needs updating.
  429.      If it does, update all the intermediate files we depend on.  */
  430.  
  431.   if (must_make)
  432.     {
  433.       for (d = file->deps; d != 0; d = d->next)
  434.     if (d->file->intermediate)
  435.       {
  436.         time_t mtime = file_mtime (d->file);
  437.         check_renamed (d->file);
  438.         d->file->parent = file;
  439.         dep_status |= update_file (d->file, depth);
  440.         check_renamed (d->file);
  441.  
  442.         {
  443.           register struct file *f = d->file;
  444.           if (f->double_colon)
  445.         f = f->double_colon;
  446.           do
  447.         {
  448.           running |= (f->command_state == cs_running
  449.                   || f->command_state == cs_deps_running);
  450.           f = f->prev;
  451.         }
  452.           while (f != 0);
  453.         }
  454.  
  455.         if (dep_status != 0 && !keep_going_flag)
  456.           break;
  457.  
  458.         if (!running)
  459.           d->changed = ((file->phony && file->cmds != 0)
  460.                 || file_mtime (d->file) != mtime);
  461.       }
  462.     }
  463.  
  464.   file->updating = 0;
  465.  
  466.   DEBUGPR ("Finished dependencies of target file `%s'.\n");
  467.  
  468.   if (running)
  469.     {
  470.       set_command_state (file, cs_deps_running);
  471.       --depth;
  472.       DEBUGPR ("The dependencies of `%s' are being made.\n");
  473.       return 0;
  474.     }
  475.  
  476.   /* If any dependency failed, give up now.  */
  477.  
  478.   if (dep_status != 0)
  479.     {
  480.       file->update_status = dep_status;
  481.       notice_finished_file (file);
  482.  
  483.       depth--;
  484.  
  485.       DEBUGPR ("Giving up on target file `%s'.\n");
  486.  
  487.       if (depth == 0 && keep_going_flag
  488.       && !just_print_flag && !question_flag)
  489.     error ("Target `%s' not remade because of errors.", file->name);
  490.  
  491.       return dep_status;
  492.     }
  493.  
  494.   if (file->command_state == cs_deps_running)
  495.     /* The commands for some deps were running on the last iteration, but
  496.        they have finished now.    Reset the command_state to not_started to
  497.        simplify later bookkeeping.  It is important that we do this only
  498.        when the prior state was cs_deps_running, because that prior state
  499.        was definitely propagated to FILE's also_make's by set_command_state
  500.        (called above), but in another state an also_make may have
  501.        independently changed to finished state, and we would confuse that
  502.        file's bookkeeping (updated, but not_started is bogus state).  */
  503.     set_command_state (file, cs_not_started);
  504.  
  505.   /* Now record which dependencies are more
  506.      recent than this file, so we can define $?.  */
  507.  
  508.   deps_changed = 0;
  509.   for (d = file->deps; d != 0; d = d->next)
  510.     {
  511.       time_t d_mtime = file_mtime (d->file);
  512.       check_renamed (d->file);
  513.  
  514. #if 1    /* %%% In version 4, remove this code completely to
  515.        implement not remaking deps if their deps are newer
  516.        than their parents.    */
  517.       if (d_mtime == (time_t) -1 && !d->file->intermediate)
  518.     /* We must remake if this dep does not
  519.        exist and is not intermediate.  */
  520.     must_make = 1;
  521. #endif
  522.  
  523.       /* Set DEPS_CHANGED if this dep actually changed.  */
  524.       deps_changed |= d->changed;
  525.  
  526.       /* Set D->changed if either this dep actually changed,
  527.      or its dependent, FILE, is older or does not exist.  */
  528.       d->changed |= noexist || d_mtime > this_mtime;
  529.  
  530.       if (debug_flag && !noexist)
  531.     {
  532.       print_spaces (depth);
  533.       if (d_mtime == (time_t) -1)
  534.         printf ("Dependency `%s' does not exist.\n", dep_name (d));
  535.       else
  536.         printf ("Dependency `%s' is %s than dependent `%s'.\n",
  537.             dep_name (d), d->changed ? "newer" : "older", file->name);
  538.       fflush (stdout);
  539.     }
  540.     }
  541.  
  542.   /* Here depth returns to the value it had when we were called.  */
  543.   depth--;
  544.  
  545.   if (file->double_colon && file->deps == 0)
  546.     {
  547.       must_make = 1;
  548.       DEBUGPR ("Target `%s' is double-colon and has no dependencies.\n");
  549.     }
  550.   else if (!noexist && file->is_target && !deps_changed && file->cmds == 0)
  551.     {
  552.       must_make = 0;
  553.       DEBUGPR ("No commands for `%s' and no dependencies actually changed.\n");
  554.     }
  555.  
  556.   if (!must_make)
  557.     {
  558.       DEBUGPR ("No need to remake target `%s'.\n");
  559.       notice_finished_file (file);
  560.       return 0;
  561.     }
  562.  
  563.   DEBUGPR ("Must remake target `%s'.\n");
  564.  
  565.   /* Now, take appropriate actions to remake the file.    */
  566.   remake_file (file);
  567.  
  568.   if (file->command_state != cs_finished)
  569.     {
  570.       DEBUGPR ("Commands of `%s' are being run.\n");
  571.       return 0;
  572.     }
  573.  
  574.   switch (file->update_status)
  575.     {
  576.     case 2:
  577.       DEBUGPR ("Failed to remake target file `%s'.\n");
  578.       break;
  579.     case 0:
  580.       DEBUGPR ("Successfully remade target file `%s'.\n");
  581.       break;
  582.     case 1:
  583.       DEBUGPR ("Target file `%s' needs remade under -q.\n");
  584.       break;
  585.     default:
  586.       assert (file->update_status >= 0 && file->update_status <= 2);
  587.       break;
  588.     }
  589.  
  590.   file->updated = 1;
  591.   return file->update_status;
  592. }
  593.  
  594. /* Set FILE's `updated' flag and re-check its mtime and the mtime's of all
  595.    files listed in its `also_make' member.  Under -t, this function also
  596.    touches FILE.
  597.  
  598.    On return, FILE->update_status will no longer be -1 if it was.  */
  599.  
  600. void
  601. notice_finished_file (file)
  602.      register struct file *file;
  603. {
  604.   struct dep *d;
  605.   int ran = file->command_state == cs_running;
  606.  
  607.   file->command_state = cs_finished;
  608.   file->updated = 1;
  609.  
  610.   if (touch_flag
  611.       /* The update status will be:
  612.         -1    if this target was not remade;
  613.         0    if 0 or more commands (+ or ${MAKE}) were run and won;
  614.         1    if some commands were run and lost.
  615.      We touch the target if it has commands which either were not run
  616.      or won when they ran (i.e. status is 0).  */
  617.       && file->update_status == 0)
  618.     {
  619.       if (file->cmds != 0 && file->cmds->any_recurse)
  620.     {
  621.       /* If all the command lines were recursive,
  622.          we don't want to do the touching.  */
  623.       unsigned int i;
  624.       for (i = 0; i < file->cmds->ncommand_lines; ++i)
  625.         if (!(file->cmds->lines_flags[i] & COMMANDS_RECURSE))
  626.           goto have_nonrecursing;
  627.     }
  628.       else
  629.     {
  630.     have_nonrecursing:
  631.       if (file->phony)
  632.         file->update_status = 0;
  633.       else
  634.         /* Should set file's modification date and do nothing else.  */
  635.         file->update_status = touch_file (file);
  636.     }
  637.     }
  638.  
  639.   if (ran && !file->phony)
  640.     {
  641.       struct file *f;
  642.  
  643.       if (just_print_flag || question_flag
  644.       || (file->is_target && file->cmds == 0))
  645.     file->last_mtime = NEW_MTIME;
  646.       else
  647.     file->last_mtime = 0;
  648.  
  649.       /* Propagate the change of modification time to all the double-colon
  650.      entries for this file.  */
  651.       for (f = file->double_colon; f != 0; f = f->next)
  652.     f->last_mtime = file->last_mtime;
  653.     }
  654.  
  655.   if (ran && file->update_status != -1)
  656.     /* We actually tried to update FILE, which has
  657.        updated its also_make's as well (if it worked).
  658.        If it didn't work, it wouldn't work again for them.
  659.        So mark them as updated with the same status.  */
  660.     for (d = file->also_make; d != 0; d = d->next)
  661.       {
  662.     d->file->command_state = cs_finished;
  663.     d->file->updated = 1;
  664.     d->file->update_status = file->update_status;
  665.  
  666.     if (ran && !d->file->phony)
  667.       /* Fetch the new modification time.
  668.          We do this instead of just invalidating the cached time
  669.          so that a vpath_search can happen.  Otherwise, it would
  670.          never be done because the target is already updated.  */
  671.       (void) f_mtime (d->file, 0);
  672.       }
  673.   else if (file->update_status == -1)
  674.     /* Nothing was done for FILE, but it needed nothing done.
  675.        So mark it now as "succeeded".  */
  676.     file->update_status = 0;
  677. }
  678.  
  679. /* Check whether another file (whose mtime is THIS_MTIME)
  680.    needs updating on account of a dependency which is file FILE.
  681.    If it does, store 1 in *MUST_MAKE_PTR.
  682.    In the process, update any non-intermediate files
  683.    that FILE depends on (including FILE itself).
  684.    Return nonzero if any updating failed.  */
  685.  
  686. static int
  687. check_dep (file, depth, this_mtime, must_make_ptr)
  688.      struct file *file;
  689.      unsigned int depth;
  690.      time_t this_mtime;
  691.      int *must_make_ptr;
  692. {
  693.   register struct dep *d;
  694.   int dep_status = 0;
  695.  
  696.   ++depth;
  697.   file->updating = 1;
  698.  
  699.   if (!file->intermediate)
  700.     /* If this is a non-intermediate file, update it and record
  701.        whether it is newer than THIS_MTIME.  */
  702.     {
  703.       time_t mtime;
  704.       dep_status = update_file (file, depth);
  705.       check_renamed (file);
  706.       mtime = file_mtime (file);
  707.       check_renamed (file);
  708.       if (mtime == (time_t) -1 || mtime > this_mtime)
  709.     *must_make_ptr = 1;
  710.     }
  711.   else
  712.     {
  713.       /* FILE is an intermediate file.
  714.      Update all non-intermediate files we depend on, if necessary,
  715.      and see whether any of them is more recent than the file
  716.      on whose behalf we are checking.  */
  717.       register struct dep *lastd;
  718.       lastd = 0;
  719.       d = file->deps;
  720.       while (d != 0)
  721.     {
  722.       if (d->file->updating)
  723.         {
  724.           error ("Circular %s <- %s dependency dropped.",
  725.              file->name, d->file->name);
  726.           if (lastd == 0)
  727.         {
  728.           file->deps = d->next;
  729.           free ((char *) d);
  730.           d = file->deps;
  731.         }
  732.           else
  733.         {
  734.           lastd->next = d->next;
  735.           free ((char *) d);
  736.           d = lastd->next;
  737.         }
  738.           continue;
  739.         }
  740.  
  741.       d->file->parent = file;
  742.       dep_status |= check_dep (d->file, depth, this_mtime, must_make_ptr);
  743.       check_renamed (d->file);
  744.       if (dep_status != 0 && !keep_going_flag)
  745.         break;
  746.  
  747.       if (d->file->command_state == cs_running
  748.           || d->file->command_state == cs_deps_running)
  749.         /* Record that some of FILE's dependencies are still being made.
  750.            This tells the upper levels to wait on processing it until
  751.            the commands are finished.  */
  752.         set_command_state (file, cs_deps_running);
  753.  
  754.       lastd = d;
  755.       d = d->next;
  756.     }
  757.     }
  758.  
  759.   file->updating = 0;
  760.   return dep_status;
  761. }
  762.  
  763. /* Touch FILE.    Return zero if successful, one if not.    */
  764.  
  765. #define TOUCH_ERROR(call) return (perror_with_name (call, file->name), 1)
  766.  
  767. static int
  768. touch_file (file)
  769.      register struct file *file;
  770. {
  771.   if (!silent_flag)
  772.     {
  773.       printf ("touch %s\n", file->name);
  774.       fflush (stdout);
  775.     }
  776.  
  777. #ifndef NO_ARCHIVES
  778.   if (ar_name (file->name))
  779.     return ar_touch (file->name);
  780.   else
  781. #endif
  782.     {
  783.       int fd = open (file->name, O_RDWR | O_CREAT, 0666);
  784.  
  785.       if (fd < 0)
  786.     TOUCH_ERROR ("touch: open: ");
  787.       else
  788.     {
  789.       struct stat statbuf;
  790.       char buf;
  791.       int status;
  792.  
  793. #ifdef EINTR
  794.       do
  795. #endif
  796.         status = fstat (fd, &statbuf);
  797. #ifdef EINTR
  798.       while (status < 0 && errno == EINTR);
  799. #endif
  800.       if (status < 0)
  801.         TOUCH_ERROR ("touch: fstat: ");
  802.       /* Rewrite character 0 same as it already is.  */
  803.       if (read (fd, &buf, 1) < 0)
  804.         TOUCH_ERROR ("touch: read: ");
  805.       if (lseek (fd, 0L, 0) < 0L)
  806.         TOUCH_ERROR ("touch: lseek: ");
  807.       if (write (fd, &buf, 1) < 0)
  808.         TOUCH_ERROR ("touch: write: ");
  809.       /* If file length was 0, we just
  810.          changed it, so change it back.  */
  811.       if (statbuf.st_size == 0)
  812.         {
  813.           (void) close (fd);
  814.           fd = open (file->name, O_RDWR | O_TRUNC, 0666);
  815.           if (fd < 0)
  816.         TOUCH_ERROR ("touch: open: ");
  817.         }
  818.       (void) close (fd);
  819.     }
  820.     }
  821.  
  822.   return 0;
  823. }
  824.  
  825. /* Having checked and updated the dependencies of FILE,
  826.    do whatever is appropriate to remake FILE itself.
  827.    Return the status from executing FILE's commands.  */
  828.  
  829. static void
  830. remake_file (file)
  831.      struct file *file;
  832. {
  833.   if (file->cmds == 0)
  834.     {
  835.       if (file->phony)
  836.     /* Phony target.  Pretend it succeeded.  */
  837.     file->update_status = 0;
  838.       else if (file->is_target)
  839.     /* This is a nonexistent target file we cannot make.
  840.        Pretend it was successfully remade.    */
  841.     file->update_status = 0;
  842.       else
  843.     {
  844.       /* This is a dependency file we cannot remake.  Fail.  */
  845.       static const char msg_noparent[]
  846.         = "%sNo rule to make target `%s'%s";
  847.       static const char msg_parent[]
  848.         = "%sNo rule to make target `%s', needed by `%s'%s";
  849.       if (keep_going_flag || file->dontcare)
  850.         {
  851.           if (!file->dontcare)
  852.         {
  853.           if (file->parent == 0)
  854.             error (msg_noparent, "*** ", file->name, ".");
  855.           else
  856.             error (msg_parent, "*** ",
  857.                file->name, file->parent->name, ".");
  858.         }
  859.           file->update_status = 2;
  860.         }
  861.       else
  862.         {
  863.           if (file->parent == 0)
  864.         fatal (msg_noparent, "", file->name, "");
  865.           else
  866.         fatal (msg_parent, "", file->name, file->parent->name, "");
  867.         }
  868.     }
  869.     }
  870.   else
  871.     {
  872.       chop_commands (file->cmds);
  873.  
  874.       if (!touch_flag || file->cmds->any_recurse)
  875.     {
  876.       execute_file_commands (file);
  877.       return;
  878.     }
  879.       else
  880.     /* This tells notice_finished_file it is ok to touch the file.    */
  881.     file->update_status = 0;
  882.     }
  883.  
  884.   /* This does the touching under -t.  */
  885.   notice_finished_file (file);
  886. }
  887.  
  888. /* Return the mtime of a file, given a `struct file'.
  889.    Caches the time in the struct file to avoid excess stat calls.
  890.  
  891.    If the file is not found, and SEARCH is nonzero, VPATH searching and
  892.    replacement is done.  If that fails, a library (-lLIBNAME) is tried and
  893.    the library's actual name (/lib/libLIBNAME.a, etc.) is substituted into
  894.    FILE.  */
  895.  
  896. time_t
  897. f_mtime (file, search)
  898.      register struct file *file;
  899.      int search;
  900. {
  901.   time_t mtime;
  902.  
  903.   /* File's mtime is not known; must get it from the system.  */
  904.  
  905. #ifndef NO_ARCHIVES
  906.   if (ar_name (file->name))
  907.     {
  908.       /* This file is an archive-member reference.  */
  909.  
  910.       char *arname, *memname;
  911.       struct file *arfile;
  912.       int arname_used = 0;
  913.  
  914.       /* Find the archive's name.  */
  915.       ar_parse_name (file->name, &arname, &memname);
  916.  
  917.       /* Find the modification time of the archive itself.
  918.      Also allow for its name to be changed via VPATH search.  */
  919.       arfile = lookup_file (arname);
  920.       if (arfile == 0)
  921.     {
  922.       arfile = enter_file (arname);
  923.       arname_used = 1;
  924.     }
  925.       mtime = f_mtime (arfile, search);
  926.       check_renamed (arfile);
  927.       if (search && strcmp (arfile->name, arname))
  928.     {
  929.       /* The archive's name has changed.
  930.          Change the archive-member reference accordingly.  */
  931.  
  932.       unsigned int arlen, memlen;
  933.  
  934.       if (!arname_used)
  935.         {
  936.           free (arname);
  937.           arname_used = 1;
  938.         }
  939.  
  940.       arname = arfile->name;
  941.       arlen = strlen (arname);
  942.       memlen = strlen (memname);
  943.  
  944.       free (file->name);
  945.  
  946.       file->name = (char *) xmalloc (arlen + 1 + memlen + 2);
  947.       bcopy (arname, file->name, arlen);
  948.       file->name[arlen] = '(';
  949.       bcopy (memname, file->name + arlen + 1, memlen);
  950.       file->name[arlen + 1 + memlen] = ')';
  951.       file->name[arlen + 1 + memlen + 1] = '\0';
  952.     }
  953.  
  954.       if (!arname_used)
  955.     free (arname);
  956.       free (memname);
  957.  
  958.       if (mtime == (time_t) -1)
  959.     /* The archive doesn't exist, so it's members don't exist either.  */
  960.     return (time_t) -1;
  961.  
  962.       mtime = ar_member_date (file->name);
  963.     }
  964.   else
  965. #endif
  966.     {
  967.       mtime = name_mtime (file->name);
  968.  
  969.       if (mtime == (time_t) -1 && search)
  970.     {
  971.       /* If name_mtime failed, search VPATH.  */
  972.       char *name = file->name;
  973.       if (vpath_search (&name, &mtime)
  974.           /* Last resort, is it a library (-lxxx)?  */
  975.           || (name[0] == '-' && name[1] == 'l'
  976.           && library_search (&name, &mtime)))
  977.         {
  978.           if (mtime != 0)
  979.         /* vpath_search and library_search store zero in MTIME
  980.            if they didn't need to do a stat call for their work.  */
  981.         file->last_mtime = mtime;
  982.           rename_file (file, name);
  983.           check_renamed (file);
  984.           return file_mtime (file);
  985.         }
  986.     }
  987.     }
  988.  
  989.   /* Store the mtime into all the entries for this file.  */
  990.   if (file->double_colon)
  991.     file = file->double_colon;
  992.   do
  993.     {
  994.       file->last_mtime = mtime;
  995.       file = file->prev;
  996.     } while (file != 0);
  997.  
  998.   return mtime;
  999. }
  1000.  
  1001.  
  1002. /* Return the mtime of the file or archive-member reference NAME.  */
  1003.  
  1004. static time_t
  1005. name_mtime (name)
  1006.      register char *name;
  1007. {
  1008.   struct stat st;
  1009.  
  1010.   if (safe_stat (name, &st) < 0)
  1011.     return (time_t) -1;
  1012.  
  1013.   return (time_t) st.st_mtime;
  1014. }
  1015.  
  1016.  
  1017. /* Search for a library file specified as -lLIBNAME, searching for a
  1018.    suitable library file in the system library directories and the VPATH
  1019.    directories.  */
  1020.  
  1021. static int
  1022. library_search (lib, mtime_ptr)
  1023.      char **lib;
  1024.      time_t *mtime_ptr;
  1025. {
  1026.   static char *dirs[] =
  1027.     {
  1028. #ifndef _AMIGA
  1029.       "/lib",
  1030.       "/usr/lib",
  1031. #endif
  1032.       LIBDIR,            /* Defined by configuration.  */
  1033.       0
  1034.     };
  1035.  
  1036.   char *libname = &(*lib)[2];   /* Name without the `-l'.  */
  1037.   time_t mtime;
  1038.  
  1039.   /* Buffer to construct possible names in.  */
  1040.   char *buf = xmalloc (sizeof (LIBDIR) + 8 + strlen (libname) + 4 + 2 + 1);
  1041.   char *file, **dp;
  1042.  
  1043.   /* Look first for `libNAME.a' in the current directory.  */
  1044.  
  1045. #ifndef _AMIGA
  1046.   sprintf (buf, "lib%s.a", libname);
  1047. #else
  1048.   sprintf (buf, "%s.lib", libname);
  1049. #endif
  1050.   mtime = name_mtime (buf);
  1051.   if (mtime != (time_t) -1)
  1052.     {
  1053.       *lib = buf;
  1054.       if (mtime_ptr != 0)
  1055.     *mtime_ptr = mtime;
  1056.       return 1;
  1057.     }
  1058.  
  1059.   /* Now try VPATH search on that.  */
  1060.  
  1061.   file = buf;
  1062.   if (vpath_search (&file, mtime_ptr))
  1063.     {
  1064.       free (buf);
  1065.       *lib = file;
  1066.       return 1;
  1067.     }
  1068.  
  1069.   /* Now try the standard set of directories.  */
  1070.  
  1071.   for (dp = dirs; *dp != 0; ++dp)
  1072.     {
  1073. #ifndef _AMIGA
  1074.       sprintf (buf, "%s/lib%s.a", *dp, libname);
  1075. #else
  1076.       sprintf (buf, "%s/%s.lib", *dp, libname);
  1077. #endif
  1078.       mtime = name_mtime (buf);
  1079.       if (mtime != (time_t) -1)
  1080.     {
  1081.       *lib = buf;
  1082.       if (mtime_ptr != 0)
  1083.         *mtime_ptr = mtime;
  1084.       return 1;
  1085.     }
  1086.     }
  1087.  
  1088.   free (buf);
  1089.   return 0;
  1090. }
  1091.